package edu.xjtu.jih.Star;

import edu.xjtu.jih.common.Result;
import edu.xjtu.jih.common.Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet({
        "/star/changeStar",
        "/star/getAllStarItem",
        "/star/getAllPublishItem",
        "/star/getStarByItemId"
})
public class StarAPI extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        StarService starService=new StarService();
        req.setCharacterEncoding("UTF-8");
        String path=req.getServletPath();

        //改变收藏状态
        if ("/star/changeStar".equals(path)){
            StarDto dto = Utils.getBeanFromRequest(StarDto.class,req);
            starService.star(dto);
            System.out.println(dto.getStar());
            Utils.outJson(resp, Result.success());
        }

        //获取用户所有收藏商品
        if ("/star/getAllStarItem".equals(path)){
            StarDto dto = Utils.getBeanFromRequest(StarDto.class,req);
            List<ItemDto> itemList= starService.getAllStarItem(dto.getU_id());
            Utils.outJson(resp, Result.success(itemList));
        }

        //获取该用户发布的所有商品
        if ("/star/getAllPublishItem".equals(path)){
            StarDto dto = Utils.getBeanFromRequest(StarDto.class,req);
            List<ItemDto> itemList= starService.getAllPublishItem(dto.getU_id());
            Utils.outJson(resp, Result.success(itemList));
        }

        if("/star/getStarByItemId".equals(path)){
            // 根据item_id和u_id获取信息
            StarDto dto = Utils.getBeanFromRequest(StarDto.class,req);
            int starState= starService.getStarByItemId(dto);
            Utils.outJson(resp, Result.success(starState));
        }

    }
}
